<?php 
## include required files
/*******************************/
require_once '../../includefiles.php';
require_once '../../model/admindetails.php';
include_once '../../model/email.php';
include_once("../../model/users.php");
require_once '../../redirect.php';

checkUserLogin();

## Create Objects
/*******************************/
$adminObj = new Model_AdminDetails();
$emailObj = new  Model_Email();
$userObj = new Model_Users();
/*******************************/
checkUserLogin();

$userId = $_SESSION['getbids_id'];

$userDetArray = $userObj->getUserDetailsByUserId($userId);
$comapnyname = str_replace(" ","_",strtolower($userDetArray['companyname']));
$userDetArray['newcomapnyname'] =$comapnyname;
$smarty->assign('userDetArray',$userDetArray);

if(isset($_POST['photo_btn']))
{   
	
		if($_FILES['photo']['name'] != '')
		{
			$size =	getimagesize($_FILES['photo']['tmp_name']);
			if($size[0] >= PROFILEIMAGEWIDTH && $size[1] >= PROFILEIMAGEHEIGHT) 
			{   
				@unlink('../../uploads/user/'.$userDetArray['user_avatar']);
				$path = '../../uploads/user/'; 
				$imageName    = $_FILES['photo']['name']; 

				$arrImageName = explode(".",$imageName);
				$position     = count($arrImageName);
				
				$fileName     = date('Ymdhis').".".$arrImageName[$position-1]; 
				@copy($_FILES['photo']['tmp_name'], $path.$fileName);

				
				$PassArray				= array();
				$PassArray['user_avatar']	= $fileName;
				$userObj->editUserValueById($PassArray,$userId);
				$_SESSION['msg'] = "<div class='success_msg'><span>Your photo updated successfully.</span></div>";		
				if($_SESSION['user_type'] == '3')		
					redirect_to_link(SITE_URL.'/myaccount/');
				else	
					redirect_to_link(SITE_URL.'/profile/'.strtolower($comapnyname));
				exit;
			}
			else
			{
			   $err = true;
			   $_SESSION['error'] = PROFILEIMAGE_WIDTHHEIGHT;
			}
		}			
		else
		{
			$_SESSION['msg'] = "<div class='error_msg'><span>All fields are mandatory.</span></div>";				
		}
}

if($_SESSION['msg'] != ''){
	$smarty->assign("msg", $_SESSION['msg']);	
	unset($_SESSION['msg']);
}

if($_SESSION['error'] != ''){
	$smarty->assign("errorMsg", $_SESSION['error']);	
	unset($_SESSION['error']);
}

$active = "myaccount";
$smarty->assign("active", $active);
$smarty->assign("subactive", $active);
$smarty->assign('mainmenu', '1');
## Unset all the objects created which are on this page
unset($userObj);

$smarty->display(TEMPLATEDIR.'/modules/myaccount/changephoto.tpl');
unset($smarty);
?>
